package com.coffee.admin.service.merchant.impl;

import com.coffee.admin.api.merchant.query.ConfigureQuery;
import com.coffee.admin.api.merchant.result.ConfigureResult;
import com.coffee.admin.api.merchant.service.ConfigureService;
import com.coffee.admin.service.merchant.bean.Configure;
import com.coffee.admin.service.merchant.dao.ConfigureDao;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;


@DubboService(dynamic = true)
public class ConfigureServiceImpl implements ConfigureService {
	@Resource
	private ConfigureDao configureDao;

	@Override
	public Page<ConfigureResult> getPager(ConfigureQuery param, PagerInfo pr) {
		Page<ConfigureResult> pager=new Page<ConfigureResult>(pr.getStart(), pr.getPageSize());
		Integer count=configureDao.count(param);
		pager.setTotalPage(count);
		if(count>0) {
			List<Configure> list=configureDao.page(param, pr.getStart(), pr.getPageSize());
			pager.setData(DTOUtils.convertList(list, ConfigureResult.class));
		}
		return pager;
	}
	
	@Override
	public Integer count(ConfigureQuery param){
		Integer count=configureDao.count(param);
		return count;
	}
	
	@Override
	public ConfigureResult getDetail(Integer id){
		Configure item=configureDao.findById(id);
		if(item==null){
			return null;
		}
		ConfigureResult result=new ConfigureResult();
		BeanUtilExt.copyProperties(result, item);
		
		return result;
	}

	@Override
	public List<ConfigureResult> getList(ConfigureQuery param, PagerInfo pr){
		List<Configure> list=configureDao.page(param, pr.getStart(), pr.getPageSize());
		return DTOUtils.convertList(list, ConfigureResult.class);
	}
	
	@Override
	public List<ConfigureResult> getList(ConfigureQuery param){
		List<Configure> list=configureDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, ConfigureResult.class);
	}
	
	@Override
	public List<ConfigureResult> getList(){
		ConfigureQuery param = new ConfigureQuery();
		List<Configure> list=configureDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, ConfigureResult.class);
	}


	@Override
	public void delete(Integer id) {
		configureDao.deleteById(id);
	}

	@Override
	public void addOrUpdate(ConfigureResult result)throws BusinessException {
		Configure item=new Configure();
		BeanUtilExt.copyProperties(item, result);
		if(item.getId()==null) {
			configureDao.insert(item);
		}else {
			Configure tmp=configureDao.findById(item.getId());
			if(tmp==null) {
				throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
			}
			configureDao.updateById(item);
		}
	}
	
	@Override
	public Map<Integer, ConfigureResult> getItemMap(Set<Integer> idSet){
		Map<Integer, ConfigureResult> map=new HashMap<Integer, ConfigureResult>();
		List<Configure> list=configureDao.findByIds(StringUtil.setToList(idSet));
		List<ConfigureResult> resultList= DTOUtils.convertList(list, ConfigureResult.class);
		for(ConfigureResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}
	
	@Override
	public Map<Integer, ConfigureResult> getItemMap(){
		Map<Integer, ConfigureResult> map=new HashMap<Integer, ConfigureResult>();
		ConfigureQuery param=new ConfigureQuery();
		List<Configure> list=configureDao.page(param, 0, Integer.MAX_VALUE);
		List<ConfigureResult> resultList= DTOUtils.convertList(list, ConfigureResult.class);
		for(ConfigureResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}

	@Override
	public Map<String, List<ConfigureResult>> getItemMapList() {
		ConfigureQuery param=new ConfigureQuery();
		List<Configure> list=configureDao.page(param, 0, Integer.MAX_VALUE);
		List<ConfigureResult> resultList= DTOUtils.convertList(list, ConfigureResult.class);
		Map<String, List<ConfigureResult>> listMap = resultList.stream().collect(Collectors.groupingBy(ConfigureResult::getName));
		return listMap;
	}


}
